package com.konsung.greendaoUtil;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.konsung.util.BroadcastUtils;

import org.greenrobot.greendao.database.Database;

import java.io.File;

/**
 * 类功能： 数据库升级
 */

public class MySQLiteOpenHelper extends DaoMaster.OpenHelper {
    private boolean mainTmpDirSet;
    private Context context;
    public static boolean isUploadDb = false;

    /**
     * 构造器
     * @param context 上下文
     * @param name 表名
     */
    public MySQLiteOpenHelper(Context context, String name) {
        super(context, name);
        this.context = context;
    }

    /**
     * 构造器
     * @param context 上下文
     * @param name 表名
     * @param factory CursorFactory
     */
    public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
        super(context, name, factory);
        this.context = context;
    }

    @Override
    public void onUpgrade(Database db, int oldVersion, int newVersion) {
        //数据迁移模块
        Log.d("GreenDao", "oldVersion-->" + oldVersion + ", newVersion-->" + newVersion);
        if (oldVersion != newVersion) {
            BroadcastUtils.sendUploadDB();
            isUploadDb = true;
        }
        int upgradeVersion  = oldVersion;

        if (1 == upgradeVersion) {
            db.execSQL("alter table " + MeasureDataBeanDao.TABLENAME
                    + " ADD ECG_IS_MEASURE INTEGER NOT NULL Default 0");
            String up = "UPDATE " + MeasureDataBeanDao.TABLENAME
                    + " SET ECG_IS_MEASURE = 1 WHERE ECG_HR != -1000";
            db.execSQL(up);
            upgradeVersion = 2;
        }
        //病人bean中增加两个字段
        if (2 == upgradeVersion) {
            db.execSQL("alter table " + PatientBeanDao.TABLENAME
                    + " ADD IS_FILE_CREAT INTEGER NOT NULL Default 0");
            db.execSQL("alter table " + PatientBeanDao.TABLENAME
                    + " ADD IS_OTHER_NET_SUCCESS INTEGER NOT NULL Default 0");
            upgradeVersion = 3;
        }
        //增加配置表
        if (3 == upgradeVersion) {
            Log.d("test", "4444");
            db.execSQL("CREATE TABLE " + "\"CONFIG_DTO\" (" + //
                    "\"TYPE\" TEXT UNIQUE ," + // 0: type
                    "\"IS_AVAILABLE\" TEXT," + // 1: isAvailable
                    "\"VERSION\" TEXT," + // 2: version
                    "\"VALUE\" TEXT);");

            upgradeVersion = 4;
        }

        if (4 == upgradeVersion) {
            db.execSQL("alter table " + PatientBeanDao.TABLENAME
                    + " ADD IS_UPLOAD_ALL INTEGER NOT NULL Default 0");
            db.execSQL("alter table " + PatientBeanDao.TABLENAME
                    + " ADD BMP_STR TEXT");
            db.execSQL("alter table " + PatientBeanDao.TABLENAME
                    + " ADD HEAD_BMP_STR TEXT");
            db.execSQL("alter table " + MeasureDataBeanDao.TABLENAME + " ADD PATIENT_ID");
        }

        //该升级方式会暂用两倍的内存。不考虑
//        MigrationHelper.getInstance().setOldVersion(oldVersion);
//        MigrationHelper.getInstance().migrate(db, MeasureDataBeanDao.class, PatientBeanDao.class);
        if (oldVersion != newVersion) {
            BroadcastUtils.sendUploadDbSuccess();
        }
        isUploadDb = false;
    }

    @Override
    public SQLiteDatabase getReadableDatabase() {
        if (!mainTmpDirSet) {
            //该逻辑切勿删除，用于修复一个Android系统bug
            boolean rs = new File(context.getFilesDir().getAbsolutePath()).mkdir();
            super.getReadableDatabase().execSQL("PRAGMA temp_store_directory = " + context
                    .getFilesDir().getAbsoluteFile());
            mainTmpDirSet = true;
            return super.getReadableDatabase();
        }
        return super.getReadableDatabase();
    }
}
